//
//  FriendDetailsMainVC.m
//  SelfDriving
//
//  Created by apple on 16/2/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FriendDetailsMainVC.h"
#import "XLSwitchView.h"
#import "FriendDynamicVC.h"
#import "FriendPhotoViewController.h"
#import "FriendVisitorVC.h"
#import "BusinessService+User.h"
#import "ChatViewController.h"
#import "BusinessService+WD.h"
#import "ReportViewController.h"
#import "DetailsViewController.h"
#import "PersonalDetailsVC.h"
#import "ImageUtil.h"
#import "UIImage+FEBoxBlur.h"
#import "XhwPlayerVC.h"

//黑白
const float hei[] = {
    0.8f,  1.6f, 0.2f, 0, -163.9f,
    0.8f,  1.6f, 0.2f, 0, -163.9f,
    0.8f,  1.6f, 0.2f, 0, -163.9f,
    0,  0, 0, 1.0f, 0 };
@interface FriendDetailsMainVC ()<XLSwitchViewDelegate,UIActionSheetDelegate,UIAlertViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *head_img;
@property (weak, nonatomic) IBOutlet UIButton *follow_Btn;
@property (weak, nonatomic) IBOutlet UIButton *msg_Btn;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *bg_img;  //头像背景最底层ImageView
@property (strong, nonatomic) XLSwitchView *switchViews;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *decLable;
@property (weak, nonatomic) IBOutlet UIImageView *vipImg;
@property (weak, nonatomic) IBOutlet UILabel *moneryLable;
@property (weak, nonatomic) IBOutlet UIImageView *xqbgimg; //取到头像时，显示在最上层的imageView(也就是真正显示头像的imageView)
@property (weak, nonatomic) IBOutlet UILabel *sy;
@property (weak, nonatomic) IBOutlet UILabel *gzfslable;
@property (weak, nonatomic) IBOutlet UILabel *timeLable;

@property (nonatomic,strong)UIButton            *xw_followBtn;

//xhw11月8好加
@property (nonatomic, assign)BOOL hasLiked;
//xhw12月28号加
@property (nonatomic, strong)FriendDynamicVC * dynamicVC;

@end

@implementation FriendDetailsMainVC{
    LoginResponseModel *model;
    UIView *topView;
    BOOL isMe;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
    //UIStatusBarStyleDefault = 0 黑色文字，浅色背景时使用
    //UIStatusBarStyleLightContent = 1 白色文字，深色背景时使用
}

- (BOOL)prefersStatusBarHidden
{
    return YES; // 返回NO表示要显示，返回YES将hiden
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    //xhw12月28号加:同步到动态之后，在我的资料详细里面的动态也应该刷新
    [self.dynamicVC xhwReFreshData];
    
    [self load]; //xhw2018-1-25改15395
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(xhwPlay:) name:@"xhwPlay" object:nil]; //xhw2018-2-12加
    
}
- (void)xhwPlay: (NSNotification *)not {
    NSString *urlStr = not.object;
    XhwPlayerVC *playerVC = [[XhwPlayerVC alloc] init];
    AVPlayer * player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlStr]];
    playerVC.player = player;
    [self presentViewController:playerVC animated:YES completion:nil];

}

- (UIImage*)grayImage:(UIImage*)sourceImage {
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil, width, height,8,0, colorSpace,kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    if (context ==NULL) {
        return nil;
    }
    CGContextDrawImage(context,CGRectMake(0,0, width, height), sourceImage.CGImage);
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    return grayImage;
}

///加模糊效果，image是图片，blur是模糊度
- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    //模糊度,
    if ((blur < 0.1f) || (blur > 2.0f)) {
        blur = 0.5f;
    }
    
    //boxSize必须大于0
    int boxSize = (int)(blur * 100);
    boxSize -= (boxSize % 2) + 1;
    NSLog(@"boxSize:%i",boxSize);
    //图像处理
    CGImageRef img = image.CGImage;
    //需要引入#import <Accelerate/Accelerate.h>
    /*
     This document describes the Accelerate Framework, which contains C APIs for vector and matrix math, digital signal processing, large number handling, and image processing.
     本文档介绍了Accelerate Framework，其中包含C语言应用程序接口（API）的向量和矩阵数学，数字信号处理，大量处理和图像处理。
     */
    
    //图像缓存,输入缓存，输出缓存
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    //像素缓存
    void *pixelBuffer;
    
    //数据源提供者，Defines an opaque type that supplies Quartz with data.
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    // provider’s data.
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
    
    //宽，高，字节/行，data
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
    
    //像数缓存，字节行*图片高
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
    
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    
    // 第三个中间的缓存区,抗锯齿的效果
    void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
    vImage_Buffer outBuffer2;
    outBuffer2.data = pixelBuffer2;
    outBuffer2.width = CGImageGetWidth(img);
    outBuffer2.height = CGImageGetHeight(img);
    outBuffer2.rowBytes = CGImageGetBytesPerRow(img);
    
    //Convolves a region of interest within an ARGB8888 source image by an implicit M x N kernel that has the effect of a box filter.
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    
    
    if (error) {
        NSLog(@"error from convolution %ld", error);
    }
    
    //    NSLog(@"字节组成部分：%zu",CGImageGetBitsPerComponent(img));
    //颜色空间DeviceRGB
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //用图片创建上下文,CGImageGetBitsPerComponent(img),7,8
    CGContextRef ctx = CGBitmapContextCreate(
                                             outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             CGImageGetBitmapInfo(image.CGImage));
    
    //根据上下文，处理过的图片，重新组件
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    
    free(pixelBuffer);
    free(pixelBuffer2);
    CFRelease(inBitmapData);
    
    CGImageRelease(imageRef);
    return returnImage;
    
    
    
}
#pragma mark 初始化
-(void)load{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:_model.uid forKey:@"fuid"];
    //    [SVProgressHUD showWithStatus:@"加载中..."];
    [[BusinessService shareInstance]getUserDetail:dic success:^(id result) {
        [SVProgressHUD dismiss];
        
        model = result;
        
        NSLog(@"2018-1-25:%@",model.is_yinshen);
        
        
        NSTimeInterval time=[model.createtime doubleValue]/1000;
        
        NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        
        //实例化一个NSDateFormatter对象
        NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
        
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd "];
        NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];

#pragma mark --谢威修改
//        self.timeLable.text = [NSString stringWithFormat:@"100m-%@",currentDateStr];
        self.timeLable.textAlignment = NSTextAlignmentLeft;
        self.timeLable.text = @"100m"; //xhw2018-1-25改
        if ([model.remarkname trim].length ==0) {
            _nameLable.text = model.nickname;
            
        }else{
            _nameLable.text = model.remarkname;
        }
        
        
        [_xqbgimg sd_setImageWithURL:[NSURL URLWithString:model.headsmall] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _xqbgimg.image = [self grayImage:image];
            
            //处理模糊
            UIImageView  *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-10, 0, SCRE_WIDTH+30, _xqbgimg.frame.size.height)];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.image = [UIImage coreBlurImage:_xqbgimg.image withBlurNumber:1];
            imageView.clipsToBounds = YES;
            [_xqbgimg addSubview:imageView];
        }];
        
        [ValidateHelper setImage:model.headsmall img:_head_img istoux:NO];
        _decLable.text = [NSString stringWithFormat:@"%@岁 | %@ | %@",[ValidateHelper timeToAge:model.age],model.job,model.sign];
        
        _gzfslable.text = [NSString stringWithFormat:@"关注 %@ | 粉丝 %@",model.follownum,model.fansnum];
        //之前是intValue,现在改成floatValue,之前是取的numberValue现在改为直接取model.money(xhw11月2号改)
        if ([model.money floatValue] < 0) {
            _moneryLable.textColor = UIColorGreen;
            _sy.textColor = UIColorGreen;
//            _moneryLable.text = [NSString stringWithFormat:@"%@",[ValidateHelper numberFormat:model.money]];
            _moneryLable.text = [NSString stringWithFormat:@"￥%@",model.money];
            
        }else if ([model.money floatValue] > 0){
            _moneryLable.textColor = UIColorRed;
            _sy.textColor = UIColorRed;
            //            _moneryLable.text = [NSString stringWithFormat:@"%@",[ValidateHelper numberFormat:model.money]];
//            _moneryLable.text = [ValidateHelper numberFormat:model.money];
            _moneryLable.text = [NSString stringWithFormat:@"￥%@",model.money];
            
        }
        else {
            _moneryLable.textColor = UIColorBlack;
            _sy.textColor = UIColorBlack;
//            _moneryLable.text = [ValidateHelper numberFormat:model.money];
            _moneryLable.text = [NSString stringWithFormat:@"￥%@",model.money];
        }
        
        if ([model.isfriend intValue]== 1) {
            [_follow_Btn setTitle:@"取消关注" forState:UIControlStateNormal];
            [self.xw_followBtn setTitle:@"+取消关注" forState:UIControlStateNormal];
            
            
            
        }else if ([model.isfriend intValue] == 3){
            
            [_follow_Btn setTitle:@"好友" forState:UIControlStateNormal];
            [self.xw_followBtn setTitle:@"好友" forState:UIControlStateNormal];
            
        }else{
            [_follow_Btn setTitle:@"关注" forState:UIControlStateNormal];
            [self.xw_followBtn setTitle:@"+关注" forState:UIControlStateNormal];
            
        }
        if ([model.memberlevel intValue]==0) {
            
            _vipImg.hidden = YES;
        }else{
            _vipImg.hidden = NO;
        }
    } failure:^(id result) {
    }];
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    isMe = [[AppDelegate shareDelegate].user.uid isEqualToString:self.model.uid];
    
    if (isMe) {
        _follow_Btn.hidden = YES;
        _msg_Btn.hidden = YES;
    }
    //在原来没有用户头像的时候显示的背景图上加个模糊效果、即加个view(xhw11月1号改)
    UIView * bgView = [[UIView alloc]initWithFrame:_bg_img.frame];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.3;
    [_bg_img addSubview:bgView];
    
    
//    [self load];
    
    
    _head_img.layer.cornerRadius = _head_img.frame.size.width/2;;
    _head_img.layer.borderWidth = 1;
    _head_img.layer.borderColor = [UIColor whiteColor].CGColor;
    _head_img.clipsToBounds = YES;
    
    _follow_Btn.layer.cornerRadius = 4;
    _msg_Btn.layer.cornerRadius = 4;
    _scrollView.contentSize = CGSizeMake(0, SCRE_HEIGHT+_bg_img.frame.size.height-64);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    //    _scrollView.bounces = NO;
    _switchViews = [[XLSwitchView alloc] initWithFrame:CGRectMake(0,_bg_img.frame.size.height,SCRE_WIDTH,SCRE_HEIGHT-64)];
    _switchViews.backgroundColor = [UIColor clearColor];
    _switchViews.delegate = self;
    _switchViews.topWidth = 44;
    _switchViews.btnWidth = SCRE_WIDTH/3;
    _switchViews.btncolor = UIColorFromRGB(0x1DA24A);
    _switchViews.inecolor = _switchViews.btncolor;
    
    CGRect r = _switchViews.topScrollView.frame;
    r.size.height = 44;
    _switchViews.topScrollView.frame = r;
    CGRect s =  _switchViews.contentScrollView.frame;
    s.origin.y = 44;
    s.size.height = SCRE_HEIGHT- 64 - 44;
    _switchViews.contentScrollView.frame = s;
    
    [_switchViews createUserInterface];
    
    [_scrollView addSubview:_switchViews];
    _scrollView.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(togerenActions:) name:@"togerenActions" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toDetailf:) name:@"toDetailf" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toadview:) name:@"toweb" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toadview:) name:@"toadviews" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tomap:) name:@"tomaps" object:nil];
    
    //    //处理黑白
    //    UIImageView *newimg = [ [UIImageView alloc]initWithFrame:_xqbgimg.frame];
    //    newimg.image = [ImageUtil imageWithImage:_xqbgimg.image withColorMatrix:hei];
    //    _xqbgimg.image = newimg.image;
    //    CGImageRef inImageRef = [_xqbgimg.image CGImage];
    //
    //    _xqbgimg.image  = [UIImage imageWithCGImage:inImageRef scale:1 orientation:UIImageOrientationUp];
    //    //处理模糊
    //    UIImageView  *imageView=[[UIImageView alloc]initWithFrame:_xqbgimg.frame];
    //    imageView.contentMode=UIViewContentModeScaleAspectFill;
    //    imageView.image = [UIImage coreBlurImage:_xqbgimg.image withBlurNumber:1];
    //    imageView.clipsToBounds = YES;
    //    [_xqbgimg addSubview:imageView];
    
    
#pragma mark --- 谢威修改
    
    UIView  *view = [[UIView alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-44,[UIScreen mainScreen].bounds.size.width,44)];
    view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:.3];
    [self.view addSubview:view];
    
    UIButton *msgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [msgBtn setTitle:@"+消息" forState:UIControlStateNormal];
    [msgBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [msgBtn addTarget:self action:@selector(msgBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:msgBtn];
    [msgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(view);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/2);
    }];
    
    
    UIButton *followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [followBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [followBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [view addSubview:followBtn];
    self.xw_followBtn = followBtn;
    [followBtn addTarget:self action:@selector(followBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(view);
        make.left.mas_equalTo(msgBtn.mas_right);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/2);
    }];
    
    self.follow_Btn.hidden = YES;
    self.msg_Btn.hidden = YES;
    
    if (isMe) {
        
        view.hidden = YES;
        
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(xwScroll:) name:@"xwScroll" object:nil];
    
    
}

#pragma mark --谢威 接受到滚动的通知
- (void)xwScroll:(NSNotification *)nsno{
    NSDictionary *dic = nsno.userInfo;
    CGFloat y =[dic[@"key"] floatValue];
    
    NSLog(@"-----------收到的通知的%f",y);
    if (y>=20) {
        [self.scrollView setContentOffset:CGPointMake(0, 200) animated:YES];
        
    }else if (y<=0){
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
 
    }
  

    
    
    
    
}

#pragma mark -- 谢威-- 消息
- (void)msgBtnClick:(UIButton *)sender{
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    ChatViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"ChatVC"];
    searchResultDetailController.user = model;
    NSLog(@"xhw->点击'+消息'->model.id->%@",model.id);
    NSLog(@"xhw->点击'+消息'->use.id->%@",searchResultDetailController.user.id);
    if (_remarkName) {
        searchResultDetailController.remarkName = _remarkName;
    }
    else {
        
    }
    
    searchResultDetailController.image = self.head_img.image;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}


#pragma mark -- 谢威-- 关注
- (void)followBtnClick:(UIButton *)sender{
    //     好友关系 1-我关注了他 2-他关注了我 3-好友 0-相互未关注
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([model.isfriend intValue]==1 ||[model.isfriend intValue] == 3) {
        [dic setObject:@"1" forKey:@"type"];
    }else{
        [dic setObject:@"0" forKey:@"type"];
    }
    [dic setObject:model.uid forKey:@"fid"];
    [SVProgressHUD showWithStatus:@"请稍等..."];
    [[BusinessService shareInstance]follow:dic success:^(id result) {
        [SVProgressHUD dismiss];
        [self load];
        
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
    
}


-(void)tomap:(NSNotification *)not{
    NSLog(@"熊宏伟->FriendDetailsMainVC->tomap通知");
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    MapViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MapVC"];
    searchResultDetailController.model = not.object;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}
-(void)toadview:(NSNotification *)not{
    
    SVWebViewController *webC = [[SVWebViewController alloc] initWithAddress:not.object];
    webC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webC animated:YES];
}
#pragma mark 动态详情
-(void)toDetailf:(NSNotification *)not{
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                 bundle:[NSBundle mainBundle]];
    DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
    searchResultDetailController.model =  not.object;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}
#pragma mark 个人详情
-(void)togerenActions:(NSNotification *)not{
    
    __weak FriendDetailsMainVC *weakSelf = self;
    if (self.hasLiked == YES) {
        self.hasLiked = NO;
        
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                     bundle:[NSBundle mainBundle]];
        FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
        searchResultDetailController.model = not.object;
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        weakSelf.hasLiked = YES;
    });
//    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
//                                                                 bundle:[NSBundle mainBundle]];
//    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
//    searchResultDetailController.model = not.object;
//    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}

- (void)XhwTogerenActions:(DTModel *)xhwModel {
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = xhwModel;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}
-(NSInteger)numberOfItemsInSwitchView:(XLSwitchView *)switchView{
    return 3;
}
-(UIViewController *)switchView:(XLSwitchView *)switchView withViewOfItem:(NSInteger)index{
    if (index == 0) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MS" bundle:nil];
        FriendPhotoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"FriendPhotoVC"];
        vc.model = _model;
        vc.title = @"相册";
        vc.mainVC = self;
        return vc;
    }else if(index == 1){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MS" bundle:nil];
        FriendDynamicVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"FriendDynamicVC"];
        vc.model = _model;
        vc.title = @"动态";
        vc.nav = self.navigationController; //xhw2018-1-25加
        vc.mainVC = self;
        self.dynamicVC = vc;
        return vc;
    }else{
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MS" bundle:nil];
        FriendVisitorVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"FriendVisitorVC"];
        vc.uid = _model.uid;
        //xhw11月8号加
        vc.mainVC = self;
        vc.title = @"访客";
        return vc;
    }
}
-(void)ScrollAction:(NSInteger)index{
    NSNumber *indexs =  [NSNumber numberWithInteger:index];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat height = scrollView.frame.size.height;
    CGFloat contentYoffset = scrollView.contentOffset.y;
    
//    NSLog(@"移动---%f",contentYoffset);
    
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;
//    NSLog(@"height:%f contentYoffset:%f frame.y:%f",height,contentYoffset,scrollView.frame.origin.y);
    if (distanceFromBottom < height) {
        if (!topView) {
            topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, 44)];
            UIButton *backbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
            [backbtn setImage:[UIImage imageNamed:@"hyback.png"] forState:UIControlStateNormal];
            [backbtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
            [topView addSubview:backbtn];
            
            UIImageView *headimg = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-15, 44/2-20, 40, 40)];
            headimg.clipsToBounds = YES;
            headimg.layer.cornerRadius = 20;
            [ValidateHelper setImage:model.headsmall img:headimg istoux:NO];
            [topView addSubview:headimg];
            
            UIButton *morebtn = [[UIButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 70, 0, 60, 44)];
            [morebtn setImage:[UIImage imageNamed:@"hygd.png"] forState:UIControlStateNormal];
            [morebtn addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
            [topView addSubview:morebtn];
            [self.view addSubview:topView];
        }else{
            topView.hidden = NO;
        }
        if (!isMe) {
            _follow_Btn.hidden = YES;
            _msg_Btn.hidden = YES;
        }
        
        _sy.hidden = YES;
        _moneryLable.hidden = YES;
    }else if(distanceFromBottom > height){
        topView.hidden = YES;
        if (!isMe) {
            _follow_Btn.hidden = YES;
            _msg_Btn.hidden =YES;
        }
        
        _sy.hidden = NO;
        _moneryLable.hidden = NO;
        
    }
}






#pragma mark 详情
- (IBAction)xqAction:(id)sender {
    [self xiangqing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma  mark 返回
- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 更多
- (IBAction)moreAction:(UIButton *)sender {
    if ([[AppDelegate shareDelegate].user.uid intValue] == [model.uid intValue]) {
        [self xiangqing];
        return;
    }
    //    好友关系 1-我关注了他 2-他关注了我 3-好友 0-相互未关注
    if ([model.isfriend intValue]== 1) {
        UIActionSheet *cel = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"备注",@"详细资料",@"取消关注",@"拉黑举报",@"取消", nil];
        [cel showInView:self.view];
    }
    if ([model.isfriend intValue]== 2) {
        UIActionSheet *cel = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"详细资料",@"移除粉丝",@"取消", nil];
        [cel showInView:self.view];
    }
    if ([model.isfriend intValue]== 3) {
        
        if ([model.is_yinshen intValue] == 1) {  //xhw2018-1-25改bug15411:根据后台返回的是否隐身字段来判断到底显示为"对其隐身"还是"对其显示"
            UIActionSheet *cel = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"备注",@"详细资料",@"取消关注",@"拉黑举报",@"对其显示",@"取消", nil];
            [cel showInView:self.view];
        }
        
        if ([model.is_yinshen intValue] == 0)
        {
            UIActionSheet *cel = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"备注",@"详细资料",@"取消关注",@"拉黑举报",@"对其隐身",@"取消", nil];
            [cel showInView:self.view];
        }
        
//        UIActionSheet *cel = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"备注",@"详细资料",@"取消关注",@"移除粉丝",@"拉黑举报",@"对其隐身",@"取消", nil];  //xhw2018-1-17改bug15181:当两个用户为好友，没有移除粉丝按钮
        
    }
    if ([model.isfriend intValue]== 0) {
        UIActionSheet *cel = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"详细资料",@"拉黑举报",@"取消", nil];
        [cel showInView:self.view];
    }
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([model.isfriend intValue]== 1) {
        if (buttonIndex == 0){//备注
            [self beizhu];
        }
        if (buttonIndex == 1){//详细资料
            [self xiangqing];
        }
        if (buttonIndex == 2){//取消关注
            [self quxiaoguanzhu];
        }
        if (buttonIndex == 3){//拉黑举报
            [self laheijubao];
        }
    }
    if ([model.isfriend intValue]== 2) {
        if (buttonIndex == 0){//详细资料
            [self xiangqing];
        }
        if (buttonIndex == 1){//移除粉丝
            [self removefansAction];
        }
    }
    if ([model.isfriend intValue]== 0) {
        if (buttonIndex == 0){//详细资料
            [self xiangqing];
        }
        if (buttonIndex == 1){//拉黑举报
            [self laheijubao];
        }
    }
    if ([model.isfriend intValue]== 3) {
        if (buttonIndex == 0){//备注
            [self beizhu];
        }
        if (buttonIndex == 1){//详细资料
            [self xiangqing];
        }
        if (buttonIndex == 2){//取消关注
            [self quxiaoguanzhu];
        }
//        if (buttonIndex == 3){//移除粉丝
//        }
//        if (buttonIndex == 4){//拉黑举报
//            [self laheijubao];
//        }
//        if (buttonIndex == 5){//对其隐身
//            [self duiqiyings];
//        }
        if (buttonIndex == 3){//拉黑举报
            [self laheijubao];
        }
        if (buttonIndex == 4){//对其隐身
            [self duiqiyings];
        }
    }
}


#pragma mark 对其隐身
-(void)duiqiyings{
    LoginResponseModel *user = [AppDelegate shareDelegate].user;
    if ([user.memberlevel intValue] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还不是会员，不能使用该功能!" delegate:self cancelButtonTitle:@"成为会员" otherButtonTitles:@"取消", nil];
        alert.tag = 1001;
        [alert show];
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.uid forKey:@"fid"];
    if ([model.is_yinshen intValue] == 1) {  //xhw2018-1-25加
        [dic setObject:@"0" forKey:@"type"];
    }
    else {
        [dic setObject:@"1" forKey:@"type"];
    }
    
    [[BusinessService shareInstance]yinshen:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"设置成功"];
        [self load];
    }failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
    
}
-(void)tomaps:(NSNotification *)not{
    NSLog(@"熊宏伟-FriendDetailsMainVC->tomap通知");
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    MapViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MapVC"];
    searchResultDetailController.model = not.object;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}
#pragma mark 拉黑举报
-(void)laheijubao{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"拉黑后将不会收到对方发来的消息可在 ‘设置->黑名单’中移除，是否确认？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    alert.tag = 1000;
    [alert show];
}
#pragma mark 取消关注
-(void)quxiaoguanzhu{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"type"];
    [dic setObject:model.uid forKey:@"fid"];
    [[BusinessService shareInstance]follow:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"取消成功"];
        [self load]; //xhw2018-2-2加:改bug15557
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}

#pragma mark 详情
-(void)xiangqing{
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    PersonalDetailsVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"PersonalDetailsVC"];
    searchResultDetailController.model = model;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}
#pragma mark 设置备注
-(void)beizhu{
    UIAlertView *customAlertView;
    if (customAlertView==nil) {
        customAlertView = [[UIAlertView alloc] initWithTitle:@"请输入备注" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    }
    [customAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *nameField = [customAlertView textFieldAtIndex:0];
    customAlertView.tag = 1024;
    nameField.tag = 1025;
    nameField.placeholder = @"请输入1-16个字符";
    [customAlertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1000) {//拉黑举报
        if (buttonIndex == 0) {//确定
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                         bundle:[NSBundle mainBundle]];
            ReportViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"ReportViewController"];
            searchResultDetailController.uid = model.uid;
            searchResultDetailController.type = @"1";
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }else if (alertView.tag == 1001){//成为会员
        if (buttonIndex == 0) {//确定
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"VipCenteVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }else if (alertView.tag == 1024){//设置备注
        if (buttonIndex == 0) {//确定
            UITextField *nameField = [alertView textFieldAtIndex:0];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:model.uid forKey:@"fid"];
            [dic setObject:nameField.text forKey:@"remark"];
            [[BusinessService shareInstance]setRemark:dic success:^(id result) {
                [SVProgressHUD showSuccessWithStatus:@"设置成功"];
                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                [dic setObject:_model.uid forKey:@"fuid"];
                [SVProgressHUD showWithStatus:@"加载中..."];
                [[BusinessService shareInstance]getUserDetail:dic success:^(id result) {
                    [SVProgressHUD dismiss];
                    model = result;
                    _nameLable.text = model.remarkname;
                }failure:^(id result) {
                    [SVProgressHUD showErrorWithStatus:result];
                }];
            } failure:^(id result) {
                [SVProgressHUD showErrorWithStatus:result];
            }];
            
        }
    }
}
#pragma  mark关注
- (IBAction)followAction:(UIButton *)sender {
    //     好友关系 1-我关注了他 2-他关注了我 3-好友 0-相互未关注
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([model.isfriend intValue]==1 ||[model.isfriend intValue] == 3) {
        [dic setObject:@"1" forKey:@"type"];
    }else{
        [dic setObject:@"0" forKey:@"type"];
    }
    [dic setObject:model.uid forKey:@"fid"];
    [SVProgressHUD showWithStatus:@"请稍等..."];
    [[BusinessService shareInstance]follow:dic success:^(id result) {
        [SVProgressHUD dismiss];
        [self load];
        
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
    
    
    
}

#pragma  mark移除粉丝
- (IBAction)removefansAction {
    //     好友关系 1-我关注了他 2-他关注了我 3-好友 0-相互未关注
    if ([model.isfriend intValue]==2) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        
        [dic setObject:@"2" forKey:@"type"];
        
        [dic setObject:model.uid forKey:@"fid"];
        [SVProgressHUD showWithStatus:@"请稍等..."];
        [[BusinessService shareInstance]follow:dic success:^(id result) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showSuccessWithStatus:@"移除成功"];
            [self load];
            
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
        
    }
    
}



#pragma  mark 消息界面
- (IBAction)toMessageAction:(UIButton *)sender {
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    ChatViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"ChatVC"];
    searchResultDetailController.user = model;
    searchResultDetailController.image = self.head_img.image;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}




@end
